package com.mallify.common.utils;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.symmetric.DES;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * Mallify工具类
 * +----------------------------------------------------------------------
 * | HANSON [ HANSON赋能开发者，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Licensed HANSON并不是自由软件，未经许可不能去掉HANSON相关版权
 * +----------------------------------------------------------------------
 * | Author: HANSON Team <hanson0622@126.com>
 * +----------------------------------------------------------------------
 */
public class MallifyUtil {

    /**
     * 加密密码
     */
    public static String encryptPassword(String pwd, String key) {
        DES des = new DES(getDESSercretKey(key));
        byte[] result = des.encrypt(pwd);
        return Base64.encode(result);
    }

    /**
     * 解密密码
     */
    public static String decryptPassowrd(String pwd, String key) {
        DES des = new DES(getDESSercretKey(key));
        return des.decryptStr(pwd);
    }

    public static void main(String[] args) {
        System.out.println(decryptPassowrd("L8qdg72wbeQ=", "admin"));
    }

    /**
     * 字符串分割，转化为数组
     *
     * @param str 字符串
     * @return List<String>
     * @author hanson.huang
     * @since 2025-02-8
     */
    public static List<String> stringToArrayStr(String str) {
        return stringToArrayStrRegex(str, ",");
    }

    /**
     * 字符串分割，转化为数组
     *
     * @param str 字符串
     * @return List<Integer>
     * @author hanson.huang
     * @since 2025-02-13
     */
    public static List<Integer> stringToArray(String str) {
        return stringToArrayByRegex(str, ",");
    }

    /**
     * 字符串分割，转化为数组
     *
     * @param str   字符串
     * @param regex 分隔符有
     * @return List<Integer>
     * @author hanson.huang
     * @since 2025-02-13
     */
    public static List<Integer> stringToArrayByRegex(String str, String regex) {
        List<Integer> list = new ArrayList<>();
        if (str.contains(regex)) {

            String[] split = str.split(regex);

            for (String value : split) {
                if (!StringUtils.isBlank(value)) {
                    list.add(Integer.parseInt(value));
                }
            }
        } else {
            list.add(Integer.parseInt(str));
        }
        return list;
    }


    /**
     * 字符串分割，转化为数组
     *
     * @param str   字符串
     * @param regex 分隔符有
     * @return List<String>
     * @author hanson.huang
     * @since 2025-02-8
     */
    public static List<String> stringToArrayStrRegex(String str, String regex) {
        List<String> list = new ArrayList<>();
        if (str.contains(regex)) {

            String[] split = str.split(regex);

            for (String value : split) {
                if (!StringUtils.isBlank(value)) {
                    list.add(value);
                }
            }
        } else {
            list.add(str);
        }
        return list;
    }

    /**
     * 获得DES加密秘钥
     *
     * @param key
     * @return
     */
    public static byte[] getDESSercretKey(String key) {
        byte[] result = new byte[8];
        byte[] keys = null;
        keys = key.getBytes(StandardCharsets.UTF_8);
        for (int i = 0; i < 8; i++) {
            if (i < keys.length) {
                result[i] = keys[i];
            } else {
                result[i] = 0x01;
            }
        }
        return result;
    }

    /**
     * hash 转换
     *
     * @param base64 String 图片流
     * @return String
     * @author hanson.huang
     * @since 2025-01-15
     */
    public static String getBase64Image(String base64) {
        return "data:image/png;base64," + base64;
    }

    /**
     * 获取客户端ip
     *
     * @param request 参数
     * @return String
     * @author hanson.huang
     * @since 2025-01-21
     */
    public static String getClientIp(HttpServletRequest request) {
        String ip = request.getHeader("X-Forwarded-For");
        if (checkIsIp(ip)) {
            return ip;
        }

        ip = request.getHeader("X-Real-IP");
        if (checkIsIp(ip)) {
            return ip;
        }

        ip = request.getRemoteAddr();
        if ("0:0:0:0:0:0:0:1".equals(ip)) {
            //本地 localhost访问 ipv6
            ip = "127.0.0.1";
        }
        if (checkIsIp(ip)) {
            return ip;
        }

        return "";
    }

    /**
     * 检测是否为ip
     *
     * @param ip 参数
     * @return String
     * @author hanson.huang
     * @since 2025-01-21
     */
    public static boolean checkIsIp(String ip) {
        if (StringUtils.isBlank(ip)) {
            return false;
        }

        if ("unKnown".equals(ip)) {
            return false;
        }

        if ("unknown".equals(ip)) {
            return false;
        }

        return ip.split("\\.").length == 4;
    }

    /**
     * 获取fin_in_set拼装sql
     *
     * @param field String 字段
     * @param idStr String 多个值，逗号分割
     * @return String
     * @author hanson.huang
     * @since 2025-02-20
     */
    public static String getFindInSetSql(String field, String idStr) {
        List<Integer> list = stringToArray(idStr);
        ArrayList<String> sqlList = CollUtil.newArrayList();
        for (Integer value : list) {
            sqlList.add(getFindInSetSql(field, value));
        }
        return "( " + StringUtils.join(sqlList, " or ") + ")";
    }

    /**
     * 获取fin_in_set拼装sql
     *
     * @param field String 字段
     * @param value Integer 值
     * @return String
     * @author Mr.Zhang
     * @since 2025-02-20
     */
    public static String getFindInSetSql(String field, Integer value) {
        return "find_in_set(" + value + ", " + field + ")";
    }

    /**
     * 手机号脱敏处理
     * @param phone 手机号
     */
    public static String maskMobile(String phone) {
        if (StrUtil.isBlank(phone)) {
            return "";
        }
        return phone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
    }

}